//路由鉴权:就是路由能不能被访问的权限的设置 -> 全局守卫完成
//引入路由器
import router from './router';
//引入 nprogress 进度条
import Nprogress from 'nprogress';
//引入 nprogress 进度条的样式
import 'nprogress/nprogress.css';
//引入大仓库
import pinia from '@/store';
import useUserStore from './store/modules/user';
let userStore = useUserStore(pinia);
//nprogress 进度条的加载小圆球不要
Nprogress.configure({ showSpinner: false });

//存储用户未登录可以访问的路由路径
let whiteList = ['/home','/hospital/register','/hospital/detail','/hospital/notice','/hospital/close','/hospital/search'];

//添加相应的全局守卫
//全局的前置守卫
//@ts-ignore
router.beforeEach((to, from, next) => {
    //访问路由组件进入之前，进度条开始动
    Nprogress.start();
    //动态设置网页左上角的标题
    document.title = `商医通-${to.meta.title}`;

    //判断用户是否登录
    let token = userStore.userInfo.token;
    //用户登录了
    if(token) {
        next();//放行
    } else {
        //用户未登录
        if(whiteList.includes(to.path)) {
            next();//放行
        } else {
            //显示登录组件
            userStore.visiable = true;
            //跳转到首页
            next({path: '/home', query: {redirect: to.fullPath}});
            
        }
    }
});

//全局的后置守卫
//@ts-ignore
router.afterEach((to, from) => {
    //访问路由组件成功，进度条消失
    Nprogress.done();
});